<?xml version="1.0"?>
<atom:feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:html="http://www.w3.org/1999/xhtml">
  <atom:id>http://bill.welliver.org/atom/pike/development tips</atom:id>
  <atom:title type="text">electronic.alchemy :: development tips</atom:title>
  <atom:updated>2026-05-01T03:35:21-04:00</atom:updated>
  <atom:link href="http://bill.welliver.org/atom/pike/development tips" type="application/atom+xml"></atom:link>
  <atom:link href="http://bill.welliver.org/space/pike/development tips" type="text/html"></atom:link>
  <atom:link href="http://bill.welliver.org/rss/pike/development tips" type="application/rss+xml"></atom:link>
  <atom:generator uri="http://modules.gotpike.org/blahblah/Public.Syndication.ATOM" version="0.1">Public.Syndication.ATOM (Pike v8.0 release 702)</atom:generator>
  <atom:icon>http://bill.welliver.org/favicon.ico</atom:icon>
  <atom:logo>http://bill.welliver.org/static/images/alchemy.gif</atom:logo>
  <atom:subtitle type="xhtml"><html:div xmlns:html="http://www.w3.org/1999/xhtml"><html:b class="bold">Module (ie, Java "static") methods in a CMOD</html:b><html:p class="paragraph"/>
You can have INIT, EXIT and PIKEFUN blocks outside of a PIKECLASS directive. Because modules are already instantiated as objects, this allows your module to have functions as well as classes.<html:p class="paragraph"/>
<html:b class="bold">Calling pike functions from within C/CMOD code</html:b><html:p class="paragraph"/>
Here's some code for calling (whether they're written in c or pike) functions from your C language module code. Remember that you can create objects this way because create is called implicitly, just ask for the class name you want to instantiate:<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
<html:font color="red">// this code works for a function that takes one argument.&#xD;
</html:font><html:font color="red">// you'll have to modify it to get it to understand multiple args.&#xD;
</html:font><html:font color="red">// args are at top of stack&#xD;
</html:font><html:p class="paragraph"/>
<html:font color="red">// look up the function&#xD;
</html:font>push_text( <html:i><html:font color="darkred">"Sql.sql_result"</html:font></html:i>);&#xD;
SAFE_APPLY_MASTER(<html:i><html:font color="darkred">"resolv"</html:font></html:i>, 1 );<html:p class="paragraph"/>
&#xD;
<html:font color="red">// method is at top of the stack, we want the arg on top&#xD;
</html:font>stack_swap();<html:p class="paragraph"/>
&#xD;
<html:font color="red">// arg is at top, function is 1 down from the top&#xD;
</html:font>apply_svalue( Pike_sp-2, 1 );<html:p class="paragraph"/>
&#xD;
<html:font color="red">// result is at top of the stack, function is still one down&#xD;
</html:font><html:font color="red">// and we want to pop it.&#xD;
</html:font>stack_swap(); &#xD;
pop_stack();&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
To run the function "query" in the current object with one arg, already on the stack:&#xD;
<html:div class="code"><html:pre><html:pre>&#xD;
<html:font color="red">// get the identifier for the member we want to call&#xD;
</html:font><html:b><html:font color="darkgreen">int </html:font></html:b><html:b><html:font color="darkbrown">i</html:font></html:b>;&#xD;
i=find_identifier(<html:i><html:font color="darkred">"query"</html:font></html:i>, Pike_fp-&gt;current_object-&gt;prog);<html:p class="paragraph"/>
&#xD;
<html:font color="red">// now call the method with the context of the current object.&#xD;
</html:font>apply_low(Pike_fp-&gt;current_object, i, 1);&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
Another option:<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre><html:p class="paragraph"/>
   <html:font color="red">/* push arguments on stack */</html:font>&#xD;
   apply(Pike_fp-&gt;current_object, <html:i><html:font color="darkred">"the method"</html:font></html:i>, number_of_args);<html:p class="paragraph"/>
</html:pre></html:pre></html:div><html:p class="paragraph"/>
<html:p class="paragraph"/>
The return value will be on the stack, don't forget to&#xD;
pop it after you are done with it.<html:p class="paragraph"/>
&#xD;
<html:b class="bold">Working with references</html:b><html:p class="paragraph"/>
Pike utilizes garbage collection and reference counting to handle memory management of pike data objects (strings, arrays, objects, etc). When writing C/CMOD code, you need to tell pike that you explicitly want to keep data around once it passes out of your function, along with the corresponding process of telling pike that you're done with the object afterward. Pike provides methods for doing this (though some methods will add references automatically).<html:p class="paragraph"/>
<html:b class="bold">add_ref(svalue val)</html:b> is used to add a reference to a variable.&#xD;
<html:b class="bold">free_string(pike_string val)</html:b> and its corresponding friends is used to subtract references and will free the variable if there are no more references to that variable. The name can be confusing but it's best to think of it from the standpoint of your code not knowing whether other code is using your variable. You're just saying that "it's ok by me if you want to free this value." Pike will actually do that when everone who said they wanted to keep it no longer want the value.<html:p class="paragraph"/>
<html:b class="bold">Working with arguments</html:b><html:p class="paragraph"/>
<html:b class="bold">Pike_sp</html:b> is your key to working with the Pike stack. The top of the stack (ie the last argument passed to your function) is Pike_sp[-1], and the bottom of the stack (or the first argument) is Pike_sp[-args]. Therefore, to get the <html:i class="ital">n~~th argument, where 1 is the first argument, is <html:b class="bold">Pike_sp[-args + n -1]</html:b>.</html:i><html:p class="paragraph"/>
Alternately, you can use <html:b class="bold">get_all_args()</html:b> to check to make sure you have the proper number and type of arguments, and place them into variables. If your needs are not terribly complex (I'm pretty sure that it doesn't handle variable argument types or varargs), this might be a good option.<html:p class="paragraph"/>
If your function receives incorrect arguments, instead of using Pike_error, use the following:<html:p class="paragraph"/>
for too few arguments, use:<html:p class="paragraph"/>
   <html:b class="bold">SIMPLE_TOO_FEW_ARGS_ERROR</html:b>("myfunction",1);<html:p class="paragraph"/>
for bad arguments, use:<html:p class="paragraph"/>
   <html:b class="bold">SIMPLE_BAD_ARG_ERROR</html:b>("myfunction", 1, "array|mapping|whatever");<html:p class="paragraph"/>
&#xD;
<html:b class="bold">Looping through Mappings</html:b><html:p class="paragraph"/>
The <html:b class="bold">NEW_MAPPING_LOOP</html:b> macro allows you to loop through all elements in a mapping. Note that the keypair name (~~k) is required and is hard coded.<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre><html:p class="paragraph"/>
   INT32 e;&#xD;
    struct keypair *k;&#xD;
    NEW_MAPPING_LOOP(<html:b><html:font color="darkgreen">mapping</html:font></html:b><html:b><html:font color="darkbrown">-&gt;data</html:font></html:b>) {&#xD;
       k-&gt;ind  &#xD;
       k-&gt;val  <html:font color="red">// both are svalues&#xD;
</html:font>    }&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
<html:b class="bold">Debugging</html:b><html:p class="paragraph"/>
<html:i class="ital">courtesy of grubba</html:i><html:p class="paragraph"/>
If you have the broken pike running in a gdb, you can try running<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
 call gdb_backtraces()&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
as well as<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
 thread apply all bt&#xD;
</html:pre></html:pre></html:div><html:p class="paragraph"/>
&#xD;
The latter should also work when examining a core dump.<html:p class="paragraph"/>
If it isn't possible to call gdb_backtraces(), you'll probably want to examine the stack frames by hand. The top-most frame is available as<html:p class="paragraph"/>
<html:div class="code"><html:pre><html:pre>&#xD;
 p *Pike_interpreter_pointer-&gt;frame_pointer&#xD;
</html:pre></html:pre></html:div>
</html:div></atom:subtitle>
</atom:feed>
